package com.tedu.pj.health.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/9/30
 */
@Controller
public class HealthController {

    @RequestMapping("ui")
    public  String doHealthUI(){
        return "default";
    }

    @RequestMapping("/json")
    @ResponseBody
    public Map<String,Object> doHealth(){
        Map<String,Object> map =new HashMap<>();
        map.put("username", "tony");
        map.put("stats", 0);
        return map;
    }


    @RequestMapping("/doHealth")
    public ModelAndView doHealth(Model model){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("default");
        mv.addObject("username", "che");
        mv.addObject("state", "0");
        return mv;
    }

    @RequestMapping("/doprint")
    public void doprint(HttpServletResponse response)throws Exception{
        Map<String,Object> map =new HashMap<>();
        map.put("username", "tony");
        map.put("stats", 0);
        //将map对象转成字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(map);
        response.getWriter().println(s);
    }
}
